<?php
/**
 * 工单
 */
namespace app\manage\controller\consult;

use app\manage\controller\Base;

class Handle extends Base
{
	private $params;

	public function __construct(){
		parent::__construct();
		// token验证
		if(false == Base::checkToken()){
			$this->error('token error!');
		}
		// 参数去token
		//$params = $this->request->post();
		$params = input('post.');
		unset($params['token']);
		$this->params = $params;
	}

	public function submit(){
		$params = $this->params;
		$title = isset($params['title'])?$params['title']:'';
		$content = isset($params['content'])?$params['content']:'';
		$post_time = isset($params['post_time'])?$params['post_time']:'';
		$pid = isset($params['pid'])?$params['pid']:0;

		if($pid){
			$model = new \app\common\model\Consult;
			$row = $model->find($pid);
			if(!$row||$row['isover']){
				$this->error('该工单已经关闭，无法提交');
			}
		}

		if(!$title||!$content){
			$this->error('请完善标题和内容');
		}
		$query = new \app\common\query\Consult;

		$data = [
			'uid' => 0,
			'admin_id' => UID,
			'pid' => $pid,
			'title' => $title,
			'content' => $content,
			'post_time' => $post_time
		];

		if($query->saveOneData($data)){
			$this->success('提交成功');
		}else{
			$this->error('提交失败');
		}

	}

	public function close(){
		$params = $this->params;
		$id = $params['id'];
		$model = new \app\common\model\Consult;
		if($model->where(['id'=>$id])->update(['isover'=>1])){
			$this->success('关闭成功');
		}else{
			$this->error('关闭失败');
		}
	}
}